<?php

namespace App\Common\WX\App\menu_smart;


use App\Common\Log;
use App\Common\WX\App\WxAppBase;
use App\Common\WX\WxWord;
use App\Model\t_a_smart_menu;
use XWX\Common\H;

class text extends WxAppBase
{
    function funcHandle(WxWord $word, $wx_from)
    {
        Log::funcLogDebug(__METHOD__);

        $list = $count = $this->context()->db()
            ->get(t_a_smart_menu::table());
        $rnd_nums = [];
        for ($i = 0; $i < count($list); $i++)
        {
            $rnd_nums[] = $i;
        }

        $menu_index = [];
        $menu_date = ['周一', '周二', '周三', '周四', '周五', '周六'];
        for ($i = 0; $i < count($menu_date); $i++)
        {
            //随机-菜单
            $rnd = mt_rand(0, count($rnd_nums) - 1);
            $menu_index[] = $rnd_nums[$rnd];

            unset($rnd_nums[$rnd]); //删除已抽到
            $rnd_nums = array_values($rnd_nums); //下标重新排列
        }
        Log::funcLogDebug(__METHOD__, "随机字");
        Log::funcLogDebug(__METHOD__, $menu_index);


        $menu_plan = [];
        $stuffs = [];
        foreach ($menu_index as $k => $v)
        {
            $entity_smenu = t_a_smart_menu::funcArrayToEntity($list[$v]);

            //材料，整合
            $stuff_data = explode('，', $entity_smenu->smenu_stuff);
            $stuffs = array_merge($stuffs, $stuff_data);

            $menu_plan[] = "{$menu_date[$k]}：{$entity_smenu->smenu_name}";
        }

        $content = implode(PHP_EOL, $menu_plan);

        //所需要的材料
        $stuffs = array_unique($stuffs);
        $content .= PHP_EOL . PHP_EOL . implode('，', $stuffs);


        $official_account = $this->getOfficialAccount();
        $official_account->msg48()->funcSendText($wx_from, $content);
//        $reply = new \EasySwoole\WeChat\Bean\OfficialAccount\Message\Text();
//        $reply->setContent("智能菜单={$word->content()}");
    }
}